package com.sky.controller.user;

import com.sky.dto.HistoryOrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * C端订单相关接口
 */
@RestController
@RequestMapping("/user/order")
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserOrderController {

    @Autowired
    OrderService orderService;

    /**
     * 催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result<String> rush(@PathVariable Long id){
        log.info("要催的订单id: {}", id);

        return Result.success("已催单");
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/reminder/{id}")
    @ApiOperation(value = "再来一单")
    public Result<String> again(@PathVariable Long id){
        log.info("再来一单的订单id: {}", id);

        return Result.success("已下单");
    }

    /**
     * 用户下单
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单, 参数为: {}", ordersSubmitDTO);

        // 业务层
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation(value = "历史订单查询")
    public Result<PageResult> getList(HistoryOrdersPageQueryDTO historyOrdersPageQueryDTO){
        log.info("历史订单查询: {}", historyOrdersPageQueryDTO);
        PageResult pageResult = orderService.getList(historyOrdersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation(value = "查询订单详情")
    public Result<Orders> getDetail(@PathVariable Long id){
        log.info("查询订单详情, 订单id为: {}", id);
        Orders orders = orderService.getDetail(id);
        return Result.success(orders);
    }

    /**
     * 取消订单
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable Long id){
        log.info("取消订单, 订单id为: {}" ,id);
        orderService.cancel(id);
        return Result.success("订单取消成功");
    }
}
